<?php
/**
 * File:          KValidatorParser
 * Description:   -
 * Author:        Kovac "Kovacou'" Alexandre (www.kovacou.fr)
 * Last Update:   10/04/2011
 */

   class KValidatorParser {
      const DECLARE_ELEMENT = '$this->_add("%s", "%s")',
            PARAMETER = '->%s(%s)',
            END = ';',
            VARS = '$this->getVar("%s")';
      
      protected $display = "";
      
      public function __construct ($ary) {
         $PARAMETERS = array (
             "text"   => scandir (dirname (__FILE__). "/Parameter/Text/"),
             "select" => scandir (dirname (__FILE__). "/Parameter/Select/"),
             "option" => scandir (dirname (__FILE__). "/Parameter/Option/"),
             "upload" => scandir (dirname (__FILE__). "/Parameter/Upload/"),
         );

         $EXCEPTIONS = array (
             "text" => array ("sensitive", "required", "trim"),
             "select" => array ("required"),
             "option" => array ("required", "multiple", "unique"),
             "upload" => array ("required", "auto", "img")
         );
         
         $REGEX = sfYaml::load (__CLASS__.".yml");
         
         foreach (array_keys ($PARAMETERS) as $key)
            unset ($PARAMETERS[$key][0], $PARAMETERS[$key][1]);


         foreach ($ary as $name => &$values)
            if (true === isset ($values["package"])) {
               if (!file_exists ($dirElement = dirname (__FILE__). "/KValidatorElement". ucfirst (strtolower ($values["package"]) .".php")))
                  throw New Exception (sprintf("KValidatorParser - Le package <strong>%s</strong> n'existe pas.", $values["package"]));

               
               $package = $values["package"];
               unset ($values["package"]);


               $this->_set (sprintf(self::DECLARE_ELEMENT, "KValidatorElement". ucfirst($package), $name));

               
               foreach ($values as $params => &$args) {
                  $params    = strtolower ($params);
                  $arguments = "";
                  
                  if (false === in_array ("{$params}.php", $PARAMETERS[$package]) && false === in_array($params, $EXCEPTIONS[$package]))
                     throw new Exception (sprintf ("KValidatorParser - Le paramètre <strong>%s</strong> pour le package <strong>%s</strong> de l'élément <strong>%s</strong> n'existe pas.", $params, $package, $name));

                  if (true === is_array ($args))
                     throw new Exception (sprintf ("KValidatorParser - Le paramètre <strong>%s</strong> pour le package <strong>%s</strong> de l'élément <strong>%s</strong> a pour arguments un tableau. L'argument doit obligatoirement être une chaine de caractère. Cette erreur est typique au modèle YML, n'oubliez pas les quotes sur les arguments !", $params, $package, $name));
                  
                  foreach (explode(",", $args) as $arg) {
                     $arg = (string) trim ($arg);
                     
                     if (preg_match('#^\$([a-zA-Z0-9_]+)$#', $arg))
                        $arguments .= sprintf (self::VARS, substr($arg, 1));
                     elseif (is_numeric ($arg) || $arg === "true" || $arg === "false" || $arg === "null")
                        $arguments .= $arg;
                     else
                        $arguments .= "'". addslashes ($arg) . "'";

                     $arguments .= ",";
                  }
                             
                  $this->_set (sprintf(self::PARAMETER, $params, rtrim((string) $arguments, ",")));
               }

               $this->_set (self::END);
            }
      }

      public function display () {
         return "<?php \n". $this->display;
      }
      
      protected function _set ($set) {
         $this->display .= (string) $set;
      }
   }
 
/**
 * End
 * Of
 * File
 */
?>